package com.timemetro.app;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlPullParserException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.net.URISyntaxException;

/**
 * Created by localadmin on 15.05.2015.
 */


public class MapMetroSurfaceView extends SurfaceView implements SurfaceHolder.Callback, View.OnTouchListener {
    public MapMetroSurfaceView(Context context, AttributeSet attrs) throws URISyntaxException, ParserConfigurationException, XmlPullParserException, SAXException, IOException {
        super(context, attrs);
        getHolder().addCallback(this);
        setOnTouchListener(this);

    }
    public MapMetroSurfaceView(Context context)  {
        super(context);
        getHolder().addCallback(this);
    }

    private ThreadForDraw drawThread;


    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            drawThread = new ThreadForDraw(getHolder(), getResources());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        drawThread.start();
        drawThread.StartDraw();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        drawThread.StopDraw();
        boolean retry = true;
        while (retry) {
            try {
                drawThread.join();
                retry = false;
            } catch (InterruptedException e) {
                // если не получилось, то будем пытаться еще и еще
            }
        }
        // завершаем работу потока
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int pointerCount = event.getPointerCount();
        if (event.getAction() == MotionEvent.ACTION_DOWN)
        {
            ScaleAndTranslate.setStartPoint(event.getX(), event.getY());

        }
        if (event.getAction() == MotionEvent.ACTION_UP){
            ScaleAndTranslate.setFinishPoint();
            if (pointerCount == 1 ) {
                ScaleAndTranslate.setIsScaleTouchFalse();
                ManagerStation.touchingScreen(event.getX(), event.getY());
            }
        }
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            if (pointerCount == 2) {
                ScaleAndTranslate.setNewPositionScale(CustomMath.CalculateDistance(event.getX(0), event.getY(0), event.getX(1), event.getY(1)));
            }
            if (pointerCount == 1) {
                ScaleAndTranslate.setNewPositionTranslate(event.getX(), event.getY());
            }
        }
        return true;
    }
}
